package com.ymttest.business.data;

import java.math.BigDecimal;
import java.util.Map;

public class Price {
	/**
	 * 计算单条价格
	 * 
	 * @param m
	 *            需要有 iPromotionType fQuotePrice fPromotion(123) fSubsidyPrice
	 *            等必须的字段,单条记录信息
	 * @param level
	 *            用户等级
	 * @param isUserNewbie
	 *            是否新人单
	 * @return 计算后的价格
	 */
	public static BigDecimal getOneActprice(Map m, int level, boolean isUserNewbie) {
		// Logger.debug(m);
		BigDecimal actprice = null;
		String ptype = null;
		// 等级的规则待确认？ 目前不清楚
		if (level > 0 && level <= 3) {
			ptype = "fPromotion2";
		} else if (level > 3 && level <= 6) {
			ptype = "fPromotion3";
		} else {
			ptype = "fPromotion1";
		}
		// iPromotionType 1 是折扣
		if (String.valueOf(m.get("iPromotionType")).equals("1")) {
			BigDecimal price = new BigDecimal(String.valueOf(m.get("fQuotePrice")));
			BigDecimal fp = null;
			if (m.get(ptype) != null) {
				fp = new BigDecimal(String.valueOf(m.get(ptype)));
			} else {
				fp = new BigDecimal(String.valueOf(m.get("fPromotion")));
			}
			actprice = price.multiply(fp);
			// Logger.debug(fp+" 折扣："+actprice);
		}
		// iPromotionType != 2 是直降
		else if (String.valueOf(m.get("iPromotionType")).equals("2")) {
			BigDecimal price = new BigDecimal(String.valueOf(m.get("fQuotePrice")));
			BigDecimal fp = null;
			if (m.get(ptype) != null) {
				fp = new BigDecimal(String.valueOf(m.get(ptype)));
			} else {
				fp = new BigDecimal(String.valueOf(m.get("fPromotion")));
			}
			actprice = price.subtract(fp);
			// Logger.debug(fp+" 直降："+actprice);
		}
		// iPromotionType != 3 是折后价
		else if (String.valueOf(m.get("iPromotionType")).equals("3")) {
			BigDecimal fp = null;
			if (m.get("fPromotion1") != null) {
				fp = new BigDecimal(String.valueOf(m.get(ptype)));
			} else {
				fp = new BigDecimal(String.valueOf(m.get("fPromotion")));
			}
			actprice = fp;
			// Logger.debug("折后价格："+fp);
		} else {
			actprice = new BigDecimal(String.valueOf(m.get("fQuotePrice")));
			// Logger.debug("默认："+String.valueOf(m.get("fQuotePrice")));
		}
		// 新人直接给新人价格
		if (isUserNewbie) {
			actprice = new BigDecimal(String.valueOf(m.get("fSubsidyPrice")));
			// Logger.debug("新人价格："+actprice);
		}
		return actprice;
	}
}
